home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / Mesa-3.0 / DEMOS / CLEARSPD.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-06-29  |  5.0 KB  |  209 lines

  1. /* $Id: clearspd.c,v 3.1 1998/06/29 02:38:30 brianp Exp $ */
  2.  
  3. /*
  4.  * Simple GLUT program to measure glClear() and glutSwapBuffers() speed.
  5.  * Brian Paul  February 15, 1997  This file in public domain.
  6.  */
  7.  
  8. /*
  9.  * $Log: clearspd.c,v $
  10.  * Revision 3.1  1998/06/29 02:38:30  brianp
  11.  * removed unneeded includes
  12.  *
  13.  * Revision 3.0  1998/02/14 18:42:29  brianp
  14.  * initial rev
  15.  *
  16.  */
  17.  
  18.  
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <math.h>
  22. #include <GL/glut.h>
  23.  
  24.  
  25. static float MinPeriod = 2.0;   /* 2 seconds */
  26. static int ColorMode = GLUT_RGB;
  27. static int Width = 400.0;
  28. static int Height = 400.0;
  29. static int Loops = 100;
  30. static float ClearColor = 0.0;
  31. static GLbitfield BufferMask = GL_COLOR_BUFFER_BIT;
  32. static GLboolean SwapFlag = GL_FALSE;
  33.  
  34.  
  35.  
  36. static void Idle( void )
  37. {
  38.    glutPostRedisplay();
  39. }
  40.  
  41.  
  42. static void Display( void )
  43. {
  44.    double t0, t1;
  45.    double clearRate;
  46.    double pixelRate;
  47.    int i;
  48.  
  49.    glClearColor( ClearColor, ClearColor, ClearColor, 0.0 );
  50.    ClearColor += 0.1;
  51.    if (ClearColor>1.0)
  52.       ClearColor = 0.0;
  53.  
  54.    if (SwapFlag) {
  55.       t0 = glutGet(GLUT_ELAPSED_TIME) * 0.001;
  56.       for (i=0;i<Loops;i++) {
  57.          glClear( BufferMask );
  58.          glutSwapBuffers();
  59.       }
  60.       t1 = glutGet(GLUT_ELAPSED_TIME) * 0.001;
  61.    }
  62.    else {
  63.       t0 = glutGet(GLUT_ELAPSED_TIME) * 0.001;
  64.       for (i=0;i<Loops;i++) {
  65.          glClear( BufferMask );
  66.       }
  67.       t1 = glutGet(GLUT_ELAPSED_TIME) * 0.001;
  68.       glutSwapBuffers();
  69.    }
  70.  
  71.    if (t1-t0 < MinPeriod) {
  72.       /* Next time do more clears to get longer elapsed time */
  73.       Loops *= 2;
  74.       return;
  75.    }
  76.  
  77.    clearRate = Loops / (t1-t0);
  78.    pixelRate = clearRate * Width * Height;
  79.    if (SwapFlag) {
  80.       printf("Rate: %d clears+swaps in %gs = %g clears+swaps/s   %d pixels/s\n",
  81.              Loops, t1-t0, clearRate, (int)pixelRate );
  82.    }
  83.    else {
  84.       printf("Rate: %d clears in %gs = %g clears/s   %d pixels/s\n",
  85.              Loops, t1-t0, clearRate, (int)pixelRate);
  86.    }
  87. }
  88.  
  89.  
  90. static void Reshape( int width, int height )
  91. {
  92.    Width = width;
  93.    Height = height;
  94.    glViewport( 0, 0, width, height );
  95.    glMatrixMode( GL_PROJECTION );
  96.    glLoadIdentity();
  97.    glOrtho(0.0, width, 0.0, height, -1.0, 1.0);
  98.    glMatrixMode( GL_MODELVIEW );
  99.    glLoadIdentity();
  100. }
  101.  
  102.  
  103. static void Key( unsigned char key, int x, int y )
  104. {
  105.    switch (key) {
  106.       case 27:
  107.          exit(0);
  108.          break;
  109.    }
  110.    glutPostRedisplay();
  111. }
  112.  
  113.  
  114. static void Init( int argc, char *argv[] )
  115. {
  116.    int i;
  117.    for (i=1; i<argc; i++) {
  118.       if (strcmp(argv[i],"+rgb")==0)
  119.          ColorMode = GLUT_RGB;
  120.       else if (strcmp(argv[i],"+ci")==0)
  121.          ColorMode = GLUT_INDEX;
  122.       else if (strcmp(argv[i],"-color")==0)
  123.          BufferMask = 0;
  124.       else if (strcmp(argv[i],"+depth")==0)
  125.          BufferMask |= GL_DEPTH_BUFFER_BIT;
  126.       else if (strcmp(argv[i],"+alpha")==0)
  127.          ColorMode = GLUT_RGB | GLUT_ALPHA;
  128.       else if (strcmp(argv[i],"+stencil")==0)
  129.          BufferMask |= GL_STENCIL_BUFFER_BIT;
  130.       else if (strcmp(argv[i],"+accum")==0)
  131.          BufferMask |= GL_ACCUM_BUFFER_BIT;
  132.       else if (strcmp(argv[i],"-width")==0) {
  133.          Width = atoi(argv[i+1]);
  134.          i++;
  135.       }
  136.       else if (strcmp(argv[i],"-height")==0) {
  137.          Height = atoi(argv[i+1]);
  138.          i++;
  139.       }
  140.       else if (strcmp(argv[i],"+swap")==0) {
  141.          SwapFlag = GL_TRUE;
  142.       }
  143.       else if (strcmp(argv[i],"-swap")==0) {
  144.          SwapFlag = GL_FALSE;
  145.       }
  146.       else
  147.          printf("Unknown option: %s\n", argv[i]);
  148.    }
  149.  
  150.    if (ColorMode & GLUT_ALPHA)
  151.       printf("Mode:  RGB + Alpha\n");
  152.    else if (ColorMode==GLUT_RGB)
  153.       printf("Mode:  RGB\n");
  154.    else
  155.       printf("Mode:  Color Index\n");
  156.    printf("SwapBuffers: %s\n", SwapFlag ? "yes" : "no" );
  157.    printf("Size: %d x %d\n", Width, Height);
  158.    printf("Buffers: ");
  159.    if (BufferMask & GL_COLOR_BUFFER_BIT)  printf("color ");
  160.    if (BufferMask & GL_DEPTH_BUFFER_BIT)  printf("depth ");
  161.    if (BufferMask & GL_STENCIL_BUFFER_BIT)  printf("stencil ");
  162.    if (BufferMask & GL_ACCUM_BUFFER_BIT)  printf("accum ");
  163.    printf("\n");
  164. }
  165.  
  166.  
  167. static void Help( const char *program )
  168. {
  169.    printf("%s options:\n", program);
  170.    printf("  +rgb       RGB mode\n");
  171.    printf("  +ci        color index mode\n");
  172.    printf("  -color     don't clear color buffer\n");
  173.    printf("  +alpha     clear alpha buffer\n");
  174.    printf("  +depth     clear depth buffer\n");
  175.    printf("  +stencil   clear stencil buffer\n");
  176.    printf("  +accum     clear accum buffer\n");
  177.    printf("  +swap      also do SwapBuffers\n");
  178.    printf("  -swap      don't do SwapBuffers\n");
  179. }
  180.  
  181.  
  182. int main( int argc, char *argv[] )
  183. {
  184.    printf("For options:  %s -help\n", argv[0]);
  185.  
  186.    Init( argc, argv );
  187.  
  188.    glutInit( &argc, argv );
  189.    glutInitWindowSize( (int) Width, (int) Height );
  190.    glutInitWindowPosition( 0, 0 );
  191.  
  192.    glutInitDisplayMode( ColorMode | GLUT_DOUBLE | GLUT_DEPTH | GLUT_STENCIL | GLUT_ACCUM );
  193.  
  194.    glutCreateWindow( argv[0] );
  195.  
  196.    if (argc==2 && strcmp(argv[1],"-help")==0) {
  197.       Help(argv[0]);
  198.       return 0;
  199.    }
  200.  
  201.    glutReshapeFunc( Reshape );
  202.    glutKeyboardFunc( Key );
  203.    glutDisplayFunc( Display );
  204.    glutIdleFunc( Idle );
  205.  
  206.    glutMainLoop();
  207.    return 0;
  208. }
  209.